package com.ruoyi.record.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.record.domain.RecordSalary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.record.domain.RecordBenefitContent;
import com.ruoyi.record.mapper.RecordBenefitMapper;
import com.ruoyi.record.domain.RecordBenefit;
import com.ruoyi.record.service.IRecordBenefitService;
import com.ruoyi.common.core.text.Convert;

/**
 * 保险金Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-02-19
 */
@Service
public class RecordBenefitServiceImpl extends ServiceImpl<RecordBenefitMapper, RecordBenefit> implements IRecordBenefitService
{

    /**
     * 查询保险金
     * 
     * @param benefitId 保险金主键
     * @return 保险金
     */
    @Override
    public RecordBenefit selectRecordBenefitByBenefitId(Long benefitId)
    {
        return baseMapper.selectRecordBenefitByBenefitId(benefitId);
    }

    /**
     * 查询保险金列表
     * 
     * @param recordBenefit 保险金
     * @return 保险金
     */
    @Override
    public List<RecordBenefit> selectRecordBenefitList(RecordBenefit recordBenefit)
    {
        return baseMapper.selectRecordBenefitList(recordBenefit);
    }

    /**
     * 新增保险金
     * 
     * @param recordBenefit 保险金
     * @return 结果
     */
    @Transactional
    @Override
    public int insertRecordBenefit(RecordBenefit recordBenefit)
    {
        recordBenefit.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(recordBenefit);
    }

    /**
     * 修改保险金
     * 
     * @param recordBenefit 保险金
     * @return 结果
     */
    @Transactional
    @Override
    public int updateRecordBenefit(RecordBenefit recordBenefit)
    {
        recordBenefit.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(recordBenefit);
    }

    /**
     * 批量删除保险金
     * 
     * @param benefitIds 需要删除的保险金主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteRecordBenefitByBenefitIds(String benefitIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(benefitIds)));
    }

    /**
     * 删除保险金信息
     * 
     * @param benefitId 保险金主键
     * @return 结果
     */
    @Override
    public int deleteRecordBenefitByBenefitId(Long benefitId)
    {
        return baseMapper.deleteById(benefitId);
    }

    /**
     * 校验保险金信息
     *
     * @param recordBenefit 保险金信息
     * @return 结果
     */
    @Override
    public boolean checkRecordBenefit(RecordBenefit recordBenefit) {
        Long benefitId = StringUtils.isNull(recordBenefit.getBenefitId()) ? -1L : recordBenefit.getBenefitId();
        RecordBenefit info = baseMapper.selectOne(new LambdaQueryWrapper<RecordBenefit>()
                .eq(RecordBenefit::getBenefitMonth, recordBenefit.getBenefitMonth())
                .last("limit 1"));
        if (StringUtils.isNotNull(info) && info.getBenefitId().longValue() != benefitId.longValue())
        {
            return false;
        }
        return true;
    }
}
